package com.fpt.group3.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fpt.group3.form.Government;
import com.fpt.group3.service.GovernmentServiceImpl;
import com.fpt.group3.service.exception.SystemException;

/**
 * Handles requests for the application home page.
 */
@Controller
public class GovernmentController {
    /**
     * logger to log process.
     */
    private static final Logger LOGGER = LoggerFactory
            .getLogger(GovernmentController.class);
    /**
     * govOfficeRegionName to assign name of Government Office Region Name.
     */
    private String govOfficeRegionName;
    /**
     * description to assign description of Government.
     */
    private String description;
    /**
     * listCounty to assign list of Counties.
     */
    private List<String> listCounty;
    /**
     * to Set list Government to Page.
     * @param model set to Table
     * @return ListGovernment it is show list of Government
     * @throws IOException input and output exception
     * @throws SystemException exception about system
     */
    @RequestMapping(value = "/governmentOfficeRegion",
                    method = RequestMethod.GET)
    public final String government(final Model model)
                 throws IOException, SystemException {
        LOGGER.info("access Government Page");
        ApplicationContext context = new ClassPathXmlApplicationContext(
                new String[] {"Spring-Module.xml" });
        GovernmentServiceImpl govService = context.getBean(
                "governmentServiceImpl", GovernmentServiceImpl.class);
        // pass false for all region
        List<Government> listGov = govService.listGovernment(false);
        model.addAttribute("listGov", listGov);
        LOGGER.info("got data gvm");
        return "ListGovernment";
    }
    /**
     * @param model set to Table.
     * @param request to request
     * @return request to request
     * @throws IOException input output exception
     * @throws SystemException exception about system
     */
    @RequestMapping(value = "/getGovermentDetail", method = RequestMethod.POST)
    public @ResponseBody final String getGovernmentDetail(
            final Model model, final HttpServletRequest request)
                    throws IOException, SystemException {
        String strGovOfficeRegionName =
                request.getParameter("govOfficeRegionName").trim();
        String strDescription = request.getParameter("description").trim();
        LOGGER.info("access getGovernmentDetail Page");
        ApplicationContext context = new ClassPathXmlApplicationContext(
                new String[] {"Spring-Module.xml"});
        GovernmentServiceImpl govService = context.getBean(
                "governmentServiceImpl", GovernmentServiceImpl.class);
        System.out.println("Parameter");
        System.out.println(strGovOfficeRegionName);
        System.out.println(strDescription);
        String msg = "";
        listCounty = govService.getListCounty(strGovOfficeRegionName);
        System.out.println(listCounty.size());
        if (listCounty.size() != 0) {
            this.govOfficeRegionName = strGovOfficeRegionName;
            this.description = strDescription;
            LOGGER.info("got detail data");
            msg = "{\"result\": \"1\",\"message\": \"successful!\"}";
        } else {
            msg = "{\"result\": \"2\",\"message\": \"unsuccessful!\"}";
        }
        return msg;
    }
    /**
     * @param model model set to Table
     * @return GovernmentDetail
     * @throws IOException input and output
     * @throws SystemException system exception
     */
    @RequestMapping(
            value = "/governmentDetail", method = RequestMethod.GET)
    public final String governmentDetail(
            final Model model) throws IOException, SystemException {
        LOGGER.info("access Government detail Page");
        model.addAttribute("govOfficeRegionName", this.govOfficeRegionName);
        model.addAttribute("description", this.description);
        for (int i = 0; i < listCounty.size(); i++) {
            System.out.println(listCounty.get(i));
        }
        model.addAttribute("listCounty", listToString(listCounty));
        return "GovernmentDetail";
    }
    /**
     * @param listCountyPara to change list to string
     * @return string
     */
    private String listToString(final List<String> listCountyPara) {
        String str = "";
        for (int i = 0; i < listCounty.size(); i++) {
            str = str + listCounty.get(i) + "\n";
        }
        return str;
    }
}
